var imgArray = new Array();

/*添加图片*/
function addImages(url) {
    var tempId = getTimestamp();
    var html = " <div id="+tempId+">";
    if(imgArray.length>0){
        html = html+"<label class=\"col-sm-2 control-label\"></label>"
    }

    html = html + "    <a class=\"fancybox\" href="+url+">" +
               "        <img alt=\"image\" style='width: 15%' src="+url+" />" +
               "    </a>" +
               "    <button class=\"btn btn-sm btn-danger\" img-url="+url+" onclick=\"deleteImg(this,'"+tempId+"')\">" +
               "        <strong>删除</strong>" +
               "    </button>" +
               " </div>";
    //添加页面图片显示
    $('#upload-list').append(html);

    imgArray.push(url);

    console.log(JSON.stringify(imgArray));

}

//删除图片
function deleteImg(obj,tempId) {
    $('#'+tempId).remove();
    var imgUrl = $(obj).attr('img-url');
    imgArray.removeByValue(imgUrl);
    console.log(imgArray);
}

//保存商品
function editGoods() {
    var goods_name = $('#goods_name').val();
    if(checkIsNull(goods_name)){
        alertTips("请填写物料名称！");
        return false;
    }

    // var sale_city = $("#sale_city option:selected").val();
    if(imgArray.length<1){
        alertTips("请上传商品图片！");
        return false;
    }
    var goods_img = imgArray[0];
    var goods_imgs = JSON.stringify(imgArray);

    var goods_brand_no = $("#goods_brand option:selected").val();
    if(checkIsNull(goods_brand_no)){
        alertTips("请选择品牌！");
        return false;
    }

    var goods_type_no = $("#goods_type option:selected").val();
    if(checkIsNull(goods_type_no)){
        alertTips("请选择类型！");
        return false;
    }

    var goods_specifications_no = $("#goods_specifications").val();
    if(checkIsNull(goods_specifications_no)){
        alertTips("请选择物料规格！");
        return false;
    }

    var desc = $('#desc-content').val();

    $.ajax({
        type: "post",
        url: '/goods/updateGoodsInfo',
        dataType: "json",
        data: {
            'goods_id' : goods_id,
            'goods_name' : goods_name,
            'goods_img' : goods_img,
            'goods_imgs' : goods_imgs,
            'goods_brand_no' : goods_brand_no,
            'goods_type_no' : goods_type_no,
            'goods_specifications_no' : goods_specifications_no,
            'desc' : desc
        },
        success: function(resp){
            console.log(resp);
            if(resp.result==='success'){
                alertTips("更新商品信息成功！");
                location.href='/goods/toGoodsList';
            }else{
                alertTips("更新商品信息异常！");
            }
        }
    });


}

